<?php

Raise::load('core.RaiseDataType');

/**
 * RaiseStack class
 * represents a stack of items
 *
 * @author Sam-Mauris Yong <hellclanner at live dot com>
 * @license http://www.opensource.org/licenses/bsd-license New BSD License
 * @package Raise.Core.DataStructure
 * @since 1.2
 */
class RaiseStack extends RaiseDataType {

    /**
     * The internal array to store all the stack items
     * @var array
     */
    private $a = array();

    /**
     * Count caching for internal array
     * @var integer|boolean
     */
    private $count = false;

    /**
     * Create a new empty RaiseStack
     */
    public function __construct(){

    }

    /**
     * Push an item into the stack
     * @param mixed $item
     */
    public function push($item){
        array_push($this->a, $item);
        $this->count = false;
    }

    /**
     * Pop an item out of the stack and return the item
     * Note that NULL is returned if the stack is empty
     * @return mixed
     */
    public function pop(){
        if($this->count() > 0){
            $this->count = false;
            return array_pop($this->a);
        }
        return null;
    }

    /**
     * Get the item on the top of the stack
     * Note that NULL is returned if the stack is empty
     * @return mixed
     */
    public function top(){
        if($this->count() > 0){
            return end($this->a);
        }
        return null;
    }

    /**
     * Get the number of items in the stack
     * @return integer
     */
    public function count(){
        if($this->count === false){
            $this->count = count($this->a);
        }
        return $this->count;
    }
}
